package com.ubiquando.api.server.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class ClientLogin {

	private static final String GOOGLE_LOGIN_URL = "https://www.google.com/accounts/ClientLogin";
	private static final String UBI_MAIL = "ubiquando.huji@gmail.com";
	private static final String UBI_PWD = "tnbuisek123";

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List<NameValuePair> postDataBuilder = new ArrayList<NameValuePair>();

		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(GOOGLE_LOGIN_URL);

		postDataBuilder.add(new BasicNameValuePair("accountType", "GOOGLE"));
		postDataBuilder.add(new BasicNameValuePair("Email", UBI_MAIL));
		postDataBuilder.add(new BasicNameValuePair("Passwd", UBI_PWD));
		postDataBuilder.add(new BasicNameValuePair("service", "ac2dm"));
		postDataBuilder.add(new BasicNameValuePair("source",
		"huji-ubiquando-1.0"));

		try {
			post.setEntity(new UrlEncodedFormEntity(postDataBuilder));
			HttpResponse response = client.execute(post);
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));

			String line = "";
			while ((line = rd.readLine()) != null) {
				if (line.startsWith("Auth=")) {
					System.out.println(line.substring(5));
				}
			}
		} catch (IOException e) {
			return;
		}
		return;
	}

}
